package main

import (
	log "github.com/sirupsen/logrus"
	"oa-rest/common"
	"oa-rest/config"
	"oa-rest/models/user"
	"strings"
	"xorm.io/xorm"
)

func main() {
	config.InitEnv()
	db, err := common.ConnectDb()
	if err != nil {
		println(err)
	}

	defer db.Close()
	//_, err = db.Exec("drop table if exists us_user ")
	//_, err = db.Exec("drop table  us_user if exists us_user")
	//_, err = db.Exec("drop table  us_organization if exists us_organization")
	//_, err = db.Exec("drop table  us_user_organization if exists us_user_organization")
	if err != nil {
		log.Fatal(err)
	}

	db.Sync2(
		new(user.UsUser),
		new(user.UsRole),
		new(user.UsUserRole),
		new(user.UsMenu),
		new(user.UsRoleMenu),
		new(user.UsUserMenu),
		new(user.UsMenuAction),
		new(user.UsOrganization),
		new(user.UsUserOrganization),
	)

	initOrg(db)
	initUsers(db)
	initRelations(db)
}

func initOrg(db *xorm.Engine) {
	orgs := []user.UsOrganization{
		{Id: 1, Code: "fuhuizhang", Name: "专职副会长"},
		{Id: 2, Code: "mishuzhang", Name: "秘书长"},
		{Id: 3, Code: "fumishuzhang", Name: "副秘书长"},
		{Id: 4, Code: "zongheguanlibu", Name: "综合管理部"},
		{Id: 5, Code: "peixunbu", Name: "培训部"},
		{Id: 6, Code: "falvgongzuobu", Name: "法律工作部"},
		{Id: 7, Code: "caixiangongzuobu", Name: "财险工作部"},
		{Id: 8, Code: "shouxiangongzuobu", Name: "寿险工作部"},
		{Id: 9, Code: "yihanggongzuobu", Name: "银行工作部"},
	}
	_, err := db.Insert(&orgs)
	if err != nil {
		panic(err)
	}
}

func initUsers(db *xorm.Engine) {
	password := strings.ToUpper(common.EncodeMD5("123456"))
	users := []user.UsUser{
		{Id: 1, UserName: "admin", Password: password, Status: 1, RealName: "超级管理员"},
		{Id: 2, UserName: "001", Password: password, Status: 1, RealName: "张夫贵"},
		{Id: 3, UserName: "002", Password: password, Status: 1, RealName: "郑  浩"},
		{Id: 4, UserName: "003", Password: password, Status: 1, RealName: "邢业久"},
		{Id: 5, UserName: "004", Password: password, Status: 1, RealName: "任海军"},
		{Id: 6, UserName: "005", Password: password, Status: 1, RealName: "董晓燕"},
		{Id: 7, UserName: "006", Password: password, Status: 1, RealName: "苏  丹"},
		{Id: 8, UserName: "007", Password: password, Status: 1, RealName: "孙鹏然"},
		{Id: 9, UserName: "008", Password: password, Status: 1, RealName: "李叶子(oa管理员）"},
		{Id: 10, UserName: "009", Password: password, Status: 1, RealName: "财务电脑"},
		{Id: 11, UserName: "010", Password: password, Status: 1, RealName: "孙士磊（部门主任）"},
		{Id: 12, UserName: "011", Password: password, Status: 1, RealName: "张兰彬"},
		{Id: 13, UserName: "012", Password: password, Status: 1, RealName: "相  彬 （部门主任）"},
		{Id: 14, UserName: "013", Password: password, Status: 1, RealName: "郭  斌"},
		{Id: 15, UserName: "014", Password: password, Status: 1, RealName: "李书娥"},
		{Id: 16, UserName: "015", Password: password, Status: 1, RealName: "高  毅"},
		{Id: 17, UserName: "016", Password: password, Status: 1, RealName: "徐晓辉（部门主任）"},
		{Id: 18, UserName: "017", Password: password, Status: 1, RealName: "谢  静"},
		{Id: 19, UserName: "018", Password: password, Status: 1, RealName: "王庆华"},
		{Id: 20, UserName: "019", Password: password, Status: 1, RealName: "王栋"},
		{Id: 21, UserName: "020", Password: password, Status: 1, RealName: "袁庆霞（部门主任）"},
		{Id: 22, UserName: "021", Password: password, Status: 1, RealName: "于红媛"},
		{Id: 23, UserName: "022", Password: password, Status: 1, RealName: "于海芳"},
		{Id: 24, UserName: "023", Password: password, Status: 1, RealName: "朱睿（部门主任）"},
		{Id: 25, UserName: "024", Password: password, Status: 1, RealName: "司津瑞"},
		{Id: 26, UserName: "025", Password: password, Status: 1, RealName: "苗  慧"},
		{Id: 27, UserName: "026", Password: password, Status: 1, RealName: "卢  冬"},
		{Id: 28, UserName: "robot", Password: password, Status: 1, RealName: "智能机器人"},
	}
	_, err := db.Insert(&users)
	if err != nil {
		panic(err)
	}
}

func initRelations(db *xorm.Engine) {
	relations := []user.UsUserOrganization{
		{UserId: "001", OrgId: 1},
		{UserId: "002", OrgId: 2},
		{UserId: "003", OrgId: 3},
		{UserId: "004", OrgId: 3},
		{UserId: "005", OrgId: 3},
		{UserId: "005", OrgId: 4},
		{UserId: "006", OrgId: 4},
		{UserId: "007", OrgId: 4},
		{UserId: "008", OrgId: 4},
		{UserId: "009", OrgId: 4},
		{UserId: "010", OrgId: 5},
		{UserId: "011", OrgId: 5},
		{UserId: "012", OrgId: 6},
		{UserId: "013", OrgId: 6},
		{UserId: "014", OrgId: 6},
		{UserId: "015", OrgId: 6},
		{UserId: "016", OrgId: 7},
		{UserId: "017", OrgId: 7},
		{UserId: "018", OrgId: 7},
		{UserId: "019", OrgId: 7},
		{UserId: "020", OrgId: 8},
		{UserId: "021", OrgId: 8},
		{UserId: "022", OrgId: 8},
		{UserId: "023", OrgId: 9},
		{UserId: "024", OrgId: 9},
		{UserId: "025", OrgId: 9},
		{UserId: "026", OrgId: 9},
	}
	_, err := db.Insert(&relations)
	if err != nil {
		panic(err)
	}
}
